/**
 * 通用的下拉框组件
 */
define(["base/declare","jquery","widget/Component","base/Data","lib/template","text!./Select.template"],
    function(declare,$,Component,Data,Template,_tpl){
    return declare([Component],{
        constructor: function(cfg){
            this.cfg = $.extend(true,{
                width : "200px",
                height : "24px"
            },this.cfg);

            this.loaded = new $.Deferred();
            this.tplRender = Template.compile(_tpl);
            var that = this;
            this.loaded.then(function(_d){
                that._dom = $(that.tplRender($.extend(that.cfg,{data:_d,keyCol:that.cfg.keyCol,valCol:that.cfg.valCol})));
            });

        },
        renderUI : function(_ctn){
            this._loadData();
            this.container.empty();
            var that = this;
            this.loaded.then(function(){
                that.container.append(that._dom);
                that.setValue(that.cfg.value);
            });
        },
        setValue : function(_v){
            this._dom.val(_v);
        },
        getValue : function(){
            return this._dom.val();
        },
        validate : function(){
            var erros = [];
            if(this.cfg.notNull && !this.val()) {
                errors.push("该项为必填");
            }
            if(erros.length){
                return {
                    succes : false,
                    msg : erros
                }
            }
            return {
                success:true
            }
        },
        //加载数据
        _loadData : function(){
            var that = this;
            if(this.cfg.url){
                var _d = new Data({
                    url : this.cfg.url
                });

                _d.on("load",function(_d){
                    that.loaded.resolve(_d);
                });
                _d.start();
            }
        }
    });
});